<template>
    <div class="dialog__wrap" v-show="state">
        <transition name="background-fade" @after-leave="afterLeave">
            <div class="dialog__background" v-if="show"></div>
        </transition>
        <transition name="fade-move">
            <div class="dialog" v-if="show">   
                <i class="iconfont icon-guanbi dialog__head__btn" @click="cancel"></i>
                <div  class="dialog__head">
                    <!-- <img src="../common/images/cindex_30.jpg" alt=""> -->
                    <img :src="dialogConfig.src" alt="">
                </div>
                <div class="dialog__body">
                    <h1>{{dialogConfig.title}}</h1>
                    <p>{{dialogConfig.message}}</p>
                    <p>{{dialogConfig.messagemore}}</p>
                </div>
                <div class="dialog__footer">
                    <Btn @click.native="confirm" class="dialog__footer__btn dialog__footer__btn--success" v-if="dialogConfig.okText">{{dialogConfig.okText}}</Btn>
                    <Btn @click.native="cancel" class="dialog__footer__btn dialog__footer__btn--fail" v-if="dialogConfig.cancelText">{{dialogConfig.cancelText}}</Btn>
                </div>
            </div>
        </transition>
    </div>
</template>

<script>
import Btn from "./Button";

class Dialog {
  src;
  title;
  message;
  messagemore;
  okText;
  cancelText;
}

export default {
  name: "DialogModal",
  data: function() {
    return {
      dialogConfig: new Dialog(),
      state: false,
      show: false
    };
  },
  methods: {
    close() {
      this.show = false;
      // this.$emit('done');
    },
    afterLeave() {
      this.state = false;
      this.$emit("update:control", false);
    },
    confirm() {
      this.close();
      this.$emit("done", true);
    },
    cancel() {
      this.close();
      this.$emit("done", false);
    }
  },
  watch: {
    options(n, o) {
      if (n) {
        this.dialogConfig = n;
      }
    },
    control(n, o) {
      this.show = n;
      if (n) {
        this.state = n;
      }
    }
  },
  components: {
    Btn
  },
  props: ["options", "control", "done"]
};
</script>

<style lang="scss" scoped>
.dialog {
  width: 5.4rem;
  // height: 7rem;
  padding-bottom: 0.2rem;
  border-radius: 4px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate3d(-50%, -50%, 0);
  background-color: #fff;
  &__wrap {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10000;
  }
  &__background {
    height: inherit;
    width: inherit;
    background-color: rgba(0, 0, 0, 0.5);
  }
  &__head {
    width: 5rem;
    height: 3.6rem;
    line-height: 1;
    text-align: center;
    position: relative;
    margin: 0.4rem auto 0.3rem;
    img {
      // width: 5rem;
      height: 3.52rem;
      display: block;
      margin: auto;
    }
    &__btn {
      line-height: 1;
      position: absolute;
      right: 0;
      top: -0.8rem;
      font-size: 0.74rem;
      color: #fff;
      // -webkit-text-stroke-width: 0.02rem;
      width: 0.58rem;
      height: 0.58rem;
      border-radius: 50%;
    }
  }
  &__body {
    padding: 0 0.2rem;
    font-size: 0.22rem;
    font-family: "\5FAE\8F6F\96C5\9ED1";
    color: #999;
    white-space: normal;
    word-break: break-all;
    text-align: center;
    line-height: 0.36rem;
    h1 {
      color: #e50838;
      margin-bottom: 0.3rem;
      font-weight: 500;
    }
    p {
      font-size: 0.22rem;
      color: #999;
      width: 4.6rem;
      white-space: nowrap;
      text-overflow: ellipsis;
      -o-text-overflow: ellipsis;
      overflow: hidden;
      text-align: center;
      margin: 0 auto;

      // 溢出隐藏并限制2行
      // overflow: hidden;
      // text-overflow: ellipsis;
      // display: -webkit-box;
      // -webkit-box-orient: vertical;
      // -webkit-line-clamp: 2;
    }

  }
  &__footer {
    font-size: 0;
    text-align: center;
    margin-top: 0.2rem;
    &__btn {
      font-size: 0.28rem;
      line-height: 1;
      margin-bottom: 0.2rem;

      &:active {
        background-color: #f1f1f1;
        opacity: 1;
      }

      &--success {
        width: 4.6rem;
        height: 0.8rem;
        font-size: 0.28rem;
        color: #fff;
        background-color: #e50838;
        border-radius: 4px;
        vertical-align: super;

        &:active {
          background-color: #e50838;
          opacity: 1;
          color: #fff;
        }
      }
      &--fail {
        background-color: #fff;
        color: #e50838;
        width: 4.6rem;
        height: 0.8rem;
        border-radius: 4px;
        border: 1px solid #e50838 !important;
        &:active {
          background-color: #fff;
          opacity: 1;
          color: #e50838;
        }
      }
    }
  }
}
.background-fade {
  &-enter {
    opacity: 0;
  }
  &-enter-active {
    transition: opacity 0.2s;
  }
  &-enter-top {
    opacity: 1;
  }
  &-leave {
    opacity: 1;
  }
  &-leave-active {
    transition: opacity 0.2s;
    opacity: 0;
  }
}
.fade-move {
  &-enter {
    opacity: 0.6;
    top: 45%;
  }
  &-enter-active {
    transition: top 0.4s ease-in-out, opacity 0.4s ease-in-out;
  }
  &-enter-to {
    opacity: 1;
    top: 50%;
  }
  &-leave {
  }
  &-leave-active {
  }
  &-leave-to {
  }
}
</style>